<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>教秘申请审批</title>
    <link rel="icon" href="../img/logo.png" type="image/x-icon">
    <link rel="stylesheet" type="text/css" href="../bootstrap-4.6.2-dist_and_jQuery/css/bootstrap.css">
    <link rel="stylesheet" type="text/css" href="../bootstrap-4.6.2-dist_and_jQuery/css/style.css">
    <script src="../bootstrap-4.6.2-dist_and_jQuery/js/jquery-3.7.0.js"></script>
    <script src="../bootstrap-4.6.2-dist_and_jQuery/js/bootstrap.js"></script>
</head>
<body>
    <div class="header">
        <div style="display: flex; align-items: center; margin-right: auto;">
            <img src="../img/logo2.png" alt="图片加载失败">
            <h2>教务管理系统</h2>
        </div>  
        
        <div class="option">
            <a href="教学秘书首页.html" class="daohang">课程安排</a>
            <!-- 录入课程，分配课程，分配教室 -->
        </div>
        <div class="option">
            <a href="教秘查看选课.html" class="daohang">查看选课</a>
            <!-- 查看特定老师或学生的课程，班级，年级，专业和成绩 -->
        </div>
        <div class="option">
            <a href="教秘审批申请.html" class="daohang">申请审批</a>
        </div>
        <img src="../img/message.png" alt="图片加载失败" style="padding: 5px;" id="message-img">
    </div>
    <div class="container">
        <table class="table">
            <thead>
            <tr>
                <th>申请人</th>
                <th>申请内容</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody id="applylist">

            </tbody>
        </table>
        <div style="display: flex; justify-content: center;">
            <p id="current_page"></p>
            <p style="margin-right: auto;" id="total_page"></p>
            <button class="btn btn-primary" style="margin-right: 20px;" onclick="prepage()">上一页</button>
            <button class="btn btn-primary" onclick="nextpage()">下一页</button>
        </div>
    </div>
    <div class="footer">
        教务管理系统 2023 from 摆烂小组
    </div>
    <!-- 通知信息 -->
    <div class="modal fade" id="messageModal" tabindex="-1" role="dialog" aria-labelledby="messageModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="messageModalLabel">通知信息</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                </div>
                <div class="modal-body">
                    <div id="messagelist">

                    </div>
                </div>
            </div>
        </div>
    </div>
    <!-- 提示信息 -->
    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="exampleModalLabel">提示消息</h5>
            </div>
            <div class="modal-body">
                <b id="message">这是提示信息</b>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
            </div>
            </div>
        </div>
    </div>
    <script>//显示提示信息
        function showmessage(message){
            $('#message').text(message)
            $('#exampleModal').modal('show');
        }
    </script>
    <script>
        $(function(){
            showapply();
            $("#message-img").click(function(){
                data={
                    receive_identity : "secretary",
                    receive_id : allCookies['secretary_id']
                };
                $.ajax({
                    url: "showmessage",
                    type:"POST",
                    data: data,
                    dataType:"text",
                    success:function(data){//返回值为发送人是谁,消息内容
                        var html="";
                        var arr=data.split(";");
                        for(let i = 0; i < arr.length-1; i+=2){
                            html+=  "<div class='message'>"+
                                        "<b>"+arr[i]+"</b>"+
                                        "<p>"+arr[i+1]+"</p>"+
                                    "</div>"+
                                    "<hr>";
                        }
                        if (arr == ""){
                            html+="<p>暂无消息</p>"
                        }
                        $("#messagelist").html(html);
                        $("#messageModal").modal('show'); 
                    },
                    error: function(jqXHR, textStatus, errorThrown){
                        showmessage("获取通知失败");
                        console.log(textStatus + ': ' + errorThrown);
                    }
                }); 
            });
        });
        var current_page = 1;//当前页数
        var total_page;//总页数
        var recordsperpage = 5;//每页的记录数
        var allCookies = parseCookies();

        //显示申请信息
        function showapply(){
            data={
                current_page1 : current_page,
                recordsperpage1 : recordsperpage,
                secretary_id : allCookies['secretary_id']
            };
            $.ajax({
                url: "secretaryshowapply",
                type:"POST",
                data: data,
                dataType:"text",
                success:function(data){//返回值为申请人,申请内容,老师id,messageid
                    var html="";
                    var arr=data.split(";");
                    total_page = Math.floor((arr[0]-1)/recordsperpage)+1;
                    console.log(arr);
                    for(var i = 1; i < arr.length-1; i+=4){
                        html+= "<tr>"+
                                    "<td>"+arr[i]+"</td>"+
                                    "<td>"+arr[i+1]+"</td>"+
                                    "<td>"+
                                        "<button class='btn btn-primary' style='margin-right : 10px;' onclick='judge("+arr[i+2]+","+arr[i+3]+",true)'>同意</button>"+
                                        "<button class='btn btn-danger' onclick='judge("+arr[i+2]+","+arr[i+3]+",false)'>否决</button>"+
                                    "</td>"+
                                "</tr>";

                    }
                    $("#applylist").html(html);
                    var currentPageElement = document.getElementById("current_page");
                    currentPageElement.textContent = current_page;
                    var currentPageElement = document.getElementById("total_page");
                    currentPageElement.textContent = "/"+total_page;  
                },
                error: function(jqXHR, textStatus, errorThrown){
                    showmessage("获取申请列表失败");
                    console.log(textStatus + ': ' + errorThrown);
                }
            });
        }
        // 上一页
        function prepage(){
            if (current_page > 1){
                current_page -= 1
                showapply();
            }
        }
        // 下一页 
        function nextpage(){
            if (current_page+1 <= total_page){
                current_page += 1;
                showapply();
            }
        }
        // 搜索
        function search() {
            current_page = 1;
            showapply();
        }
        // 审批
        function judge(n,messageid,flag){
            data = {
                teacherid : n,
                secretary_id : allCookies['secretary_id'],
                messageid : messageid,
                flag : flag
            };
            $.ajax({
                    url: "secretaryjudge",
                    type:"POST",
                    data: data,//给你学生id和flag,flag代表是不是通过,在消息内容里面写不同的文本
                    dataType:"text",
                    success:function(data){
                        if(data == "true"){
                            showmessage("审批成功")
                            showapply();
                        }
                    },
                    error: function(jqXHR, textStatus, errorThrown){
                        showmessage("审批失败");
                        console.log(textStatus + ': ' + errorThrown);
                    }
                }); 
        }
        function parseCookies() {
            var cookies = {};
            var cookieString = document.cookie;
            var cookieArray = cookieString.split('; ');

            for (var i = 0; i < cookieArray.length; i++) {
                var cookie = cookieArray[i].split('=');
                var name = cookie[0];
                var value = cookie[1];
                cookies[name] = value;
            }

            return cookies;
        }
    </script>
</body>    
</html>